package com.laboratory.manage.controller;

import com.laboratory.manage.entity.MemberEntity;
import com.laboratory.manage.service.MemberService;
import com.laboratory.manage.utils.PageUtils;
import com.laboratory.manage.utils.R;
import com.laboratory.manage.utils.UploadImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Arrays;
import java.util.Map;
//@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/members")
public class MemberController {
    //调用业务层

    @Autowired
    private MemberService memberService;

    /**
     * 成员增加
     */
    @PostMapping("/save")
    public R save(@RequestParam("name") String name,
                  @RequestParam("gender") String gender,
                  @RequestParam("hometown") String hometown,
                  @RequestParam("grade") String grade,
                  @RequestParam("subjectCategory") String subjectCategory,
                  @RequestParam("phone") String phone,
                  @RequestParam("email") String email,
                  @RequestParam("achievement") String achievement,
                  @RequestParam("workAddress") String workAddress,
                  @RequestParam("selfIntroduction") String selfIntroduction,
                  @RequestParam("file") MultipartFile file) throws Exception {

        if(file.isEmpty()){
            return R.error(14000, "上传图片不能为空");
        }

        //获取文件基本信息
        //获取正在处理的文件的原始文件名
        String originalFilename = file.getOriginalFilename();

        //通过将当前系统时间（以毫秒为单位）附加到原始文件名上生成一个新的文件名,这样做可以确保文件名的唯一性。
        String newName =  System.currentTimeMillis() + originalFilename;

        //调用上传图片的工具类的方法
        File uploadImage = UploadImageUtils.UploadImage(file, "member");

        file.transferTo(new File(uploadImage, newName));
        String fileType = file.getContentType();
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setName(name);
        memberEntity.setGender(gender);
        memberEntity.setAchievement(achievement);
        memberEntity.setPhone(phone);
        memberEntity.setEmail(email);
        memberEntity.setGrade(grade);
        memberEntity.setHometown(hometown);
        memberEntity.setSelfIntroduction(selfIntroduction);
        memberEntity.setSubjectCategory(subjectCategory);
        memberEntity.setWorkAddress(workAddress);
        memberEntity.setPhoto("uploadImages/" + "member" + "/" + newName);

        if(memberService.save(memberEntity)){
            return R.ok().put("data", memberEntity);
        }
        return R.error(14000, "上传失败");

    }

    /**
     * 按照数组删除资产批量或者单独删除资产信息
     */
    @RequestMapping("/delete")
    public R delete (@RequestBody Long[] ids){
        if(memberService.removeByIds(Arrays.asList(ids))){
            return R.ok();
        }else {
            return R.error(14000, "删除失败");
        }
    }

    /**
     * 修改成员信息
     */
    @RequestMapping("/update")
    public R update(@RequestParam("id") String id,
                    @RequestParam("name") String name,
                    @RequestParam("gender") String gender,
                    @RequestParam("hometown") String hometown,
                    @RequestParam("grade") String grade,
                    @RequestParam("subjectCategory") String subjectCategory,
                    @RequestParam("phone") String phone,
                    @RequestParam("email") String email,
                    @RequestParam("achievement") String achievement,
                    @RequestParam("workAddress") String workAddress,
                    @RequestParam("selfIntroduction") String selfIntroduction,
                    @RequestParam("photo") String photo,
                    @RequestParam("file") MultipartFile file) throws Exception{

        // 根据id寻找实体类
        MemberEntity memberEntity = memberService.getById(id);

        if(memberEntity == null){
            return R.error(14000, "修改失败!!");
        }

        //判断是否更改了图片
        if(!file.isEmpty()){
            //说明更改了图片，那就先保存这个图片
            //获取文件基本信息
            //获取正在处理的文件的原始文件名
            String originalFilename = file.getOriginalFilename();

            //通过将当前系统时间（以毫秒为单位）附加到原始文件名上生成一个新的文件名,这样做可以确保文件名的唯一性。
            String newName =  System.currentTimeMillis() + originalFilename;

            //调用上传图片的工具类的方法
            File uploadImage = UploadImageUtils.UploadImage(file, "member");

            file.transferTo(new File(uploadImage, newName));

            //设置图片保存的路径
            memberEntity.setPhoto("uploadImages/" + "member" + "/" + newName);
        }

        memberEntity.setName(name);

        memberEntity.setGender(gender);

        memberEntity.setAchievement(achievement);

        memberEntity.setPhone(phone);

        memberEntity.setEmail(email);

        memberEntity.setGrade(grade);

        memberEntity.setHometown(hometown);

        memberEntity.setSelfIntroduction(selfIntroduction);

        memberEntity.setSubjectCategory(subjectCategory);

        memberEntity.setWorkAddress(workAddress);

        System.out.println("photo" + memberEntity.getPhoto());
        //调用更新方法，更新这个图片
        boolean update = memberService.update(memberEntity);
        if(update){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 分页展示所有成员
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("data", page);
    }
}


